package com.huishu.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huishu.commons.pojo.BasePage;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.goods.pojo.dto.ProductCategoryDTO;
import com.huishu.goods.pojo.model.ProductCategoryDO;
import com.huishu.goods.service.ProductCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author generator@ljj
 * @since 2023-04-08
 */
@RestController
@RequestMapping("/product-category")
@Slf4j
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @PostMapping("/pageList")
    public Result pageList(@RequestBody BasePage basePage) {
        return productCategoryService.pageList(basePage);
    }

    @GetMapping("/findAll")
    public Result findAll() {
        // 查询品类
        LambdaQueryWrapper<ProductCategoryDO> queryWrapper = Wrappers.lambdaQuery(ProductCategoryDO.class)
                .eq(ProductCategoryDO::getIsShow, true)
                .orderByAsc(ProductCategoryDO::getSort);
        return Result.success(productCategoryService.list(queryWrapper));
    }

    @PostMapping("/create")
    public Result create(@RequestBody ProductCategoryDTO productCategoryDTO) {
        return productCategoryService.handle(productCategoryDTO, 1);
    }

    @PostMapping("/update")
    public Result modify(@RequestBody ProductCategoryDTO productCategoryDTO) {
        return productCategoryService.handle(productCategoryDTO, 3);
    }

    @DeleteMapping("/delete/{id}")
    public Result modify(@PathVariable Long id) {
        ProductCategoryDO productCategoryDO = productCategoryService.getById(id);
        ProductCategoryDTO productCategoryDTO = BeanUtils.copyProperties(productCategoryDO, ProductCategoryDTO.class);
        return productCategoryService.handle(productCategoryDTO, 2);
    }
}
